package com.masterclient.app.modules.chezhubang;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.masterclient.app.R;
import com.eoner.baselibrary.utils.ScreenUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class SelectMapDialog extends Dialog {


    @BindView(R.id.dialog_gd)
    TextView dialogGd;
    @BindView(R.id.line_gd)
    View lineGd;
    @BindView(R.id.dialog_bd)
    TextView dialogBd;
    @BindView(R.id.line_bd)
    View lineBd;
    @BindView(R.id.dialog_tx)
    TextView dialogTx;
    @BindView(R.id.cancel)
    TextView cancel;

    String startLongitude, startLatitude, endLongitude, endLatitude, name;
    Context context;


    public SelectMapDialog(@NonNull Context context) {
        super(context, R.style.MyDialog);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_map_select);
        ButterKnife.bind(this);

        setCanceledOnTouchOutside(true);

    }

    public void setVisiable(boolean isGd,boolean isBd,boolean isTx){
        dialogGd.setVisibility(isGd?View.VISIBLE:View.GONE);
        lineGd.setVisibility(isGd&&(isBd||isTx)?View.VISIBLE:View.GONE);
        dialogBd.setVisibility(isBd?View.VISIBLE:View.GONE);
        lineBd.setVisibility(isBd&&isTx?View.VISIBLE:View.GONE);
        dialogTx.setVisibility(isTx?View.VISIBLE:View.GONE);
    }

    public void setData(String startLongitude,String startLatitude,String endLongitude,
                        String endLatitude,String name){
        this.startLatitude = startLatitude;
        this.startLongitude = startLongitude;
        this.endLatitude = endLatitude;
        this.endLongitude = endLongitude;
        this.name = name;
    }


    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        windowparams.width = ScreenUtils.getScreenWidth()-ScreenUtils.dp2px(20);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
        window.setWindowAnimations(R.style.bottomShow);
    }


    @OnClick({R.id.dialog_gd, R.id.dialog_bd, R.id.dialog_tx, R.id.cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.dialog_gd:
                String gdUri = "amapuri://route/plan/?sid=&slat=" +startLatitude+
                        "&slon=" + startLongitude +
                        "&sname=我的位置&did=&dlat=" + endLatitude +
                        "&dlon=" + endLongitude +
                        "&dname=" + name +
                        "&dev=0&t=0";
                Log.e("======gdUri", gdUri);
                try {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(gdUri));
                    context.startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(context, "未安装高德地图的客户端", Toast.LENGTH_LONG).show();
                }
                dismiss();

                break;
            case R.id.dialog_bd:
//                // 将google地图、soso地图、aliyun地图、mapabc地图和amap地图// 所用坐标转换成百度坐标
//                CoordinateConverter converter  = new CoordinateConverter();
//                converter.from(CoordType.COMMON);
//// sourceLatLng待转换坐标
//                converter.coord(sourceLatLng);
//                LatLng desLatLng = converter.convert();

                String bdUri = "baidumap://map/direction?origin=name:我的位置|latlng:" +startLatitude+"," + startLongitude+
                        "&destination=name:"+name+"|latlng:" +endLatitude+"," + endLongitude+
                        "&coord_type=gcj02&mode=driving&src="+ context.getPackageName();
                Log.e("======bdUri", bdUri);
                try {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(bdUri));
                    context.startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(context, "未安装百度地图的客户端", Toast.LENGTH_LONG).show();
                }
                dismiss();

                break;
            case R.id.dialog_tx:
                String txUri = "qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=" + startLatitude+"," + startLongitude+
                        "&to="+name+"&tocoord=" +endLatitude+"," + endLongitude+"&referer=";

                Log.e("======txUri", txUri);
                try {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(txUri));
                    context.startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(context, "未安装腾讯地图的客户端", Toast.LENGTH_LONG).show();
                }
                dismiss();
                break;
            case R.id.cancel:
                dismiss();
                break;
        }
    }
}
